package com.example.anan.AAChartCore.ChartsDemo.MainContent.slice;


import com.example.anan.AAChartCore.ResourceTable;
import com.example.anan.AAChartCore.expandablelist.BaseExpandableItemProvider;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;

public class MyBaseExpandableItemProvider extends BaseExpandableItemProvider {

    private String[] gData;
    private String[][] iData;
    private Context mContext;
    private String[] colorsArr = new String[]{
            "#5470c6",
            "#91cc75",
            "#fac858",
            "#ee6666",
            "#73c0de",
            "#3ba272",
            "#fc8452",
            "#9a60b4",
            "#ea7ccc",

            "#5470c6",
            "#91cc75",
            "#fac858",
            "#ee6666",
            "#73c0de",
            "#3ba272",
            "#fc8452",
            "#9a60b4",
            "#ea7ccc",
    };

    public MyBaseExpandableItemProvider(String[] gData, String[][] iData, Context mContext) {
        this.gData = gData != null ? gData.clone() : null;
        this.iData = iData != null ? iData.clone() : null;
        this.mContext = mContext;
    }


    @Override
    public int getGroupCount() {
        return gData.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return iData[groupPosition].length;
    }

    @Override
    public String getGroupItem(int groupPosition) {
        return gData[groupPosition];
    }

    @Override
    public String getChildItem(int groupPosition, int childPosition) {
        return iData[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    //取得用于显示给定分组的视图. 这个方法仅返回分组的视图对象
    @Override
    public Component getGroupComponent(int groupPosition, boolean isExpanded, Component convertView, ComponentContainer parent) {

        ViewHolderGroup groupHolder;
        if (convertView == null || !(convertView.getTag() instanceof ViewHolderGroup)) {
            convertView = LayoutScatter.getInstance(mContext).parse(
                    ResourceTable.Layout_item_exlist_group, parent, false);
            groupHolder = new ViewHolderGroup();
            groupHolder.tv_group_name = (Text) convertView.findComponentById(ResourceTable.Id_tv_group_name);
            groupHolder.image_arrow = (Image) convertView.findComponentById(ResourceTable.Id_image_arrow);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (ViewHolderGroup) convertView.getTag();
        }
        groupHolder.tv_group_name.setText(gData[groupPosition]);
        if (isExpanded) {
            groupHolder.image_arrow.setRotation(180);
        } else {
            groupHolder.image_arrow.setRotation(0);
        }
        return convertView;
    }

    //取得显示给定分组给定子位置的数据用的视图
    @Override
    public Component getChildComponent(int groupPosition, int childPosition, boolean isLastChild, Component convertView, ComponentContainer parent) {
        ViewHolderItem itemHolder;
        if (convertView == null || !(convertView.getTag() instanceof ViewHolderItem)) {
            convertView = LayoutScatter.getInstance(mContext).parse(
                    ResourceTable.Layout_item_exlist_item, parent, false);
            itemHolder = new ViewHolderItem();
            itemHolder.tv_color_dot = (Text) convertView.findComponentById(ResourceTable.Id_tv_color_dot);
            itemHolder.tv_name = (Text) convertView.findComponentById(ResourceTable.Id_tv_name);
            convertView.setTag(itemHolder);
        } else {
            itemHolder = (ViewHolderItem) convertView.getTag();
        }
        String colorStr = colorsArr[groupPosition];
        int intColor = Color.getIntColor(colorStr);
        Color color = new Color(intColor);
        itemHolder.tv_color_dot.setTextColor(color);
        itemHolder.tv_name.setText(iData[groupPosition][childPosition]);

        return convertView;
    }

    //设置子列表是否可选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    private static class ViewHolderGroup {
        private Text tv_group_name;
        private Image image_arrow;
    }

    private static class ViewHolderItem {
        private Text tv_color_dot;
        private Text tv_name;
    }

}
